package com.tuanbiao.presenter;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.UIDRuleEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.EncodingRulesController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.data.entity.CodeModelType;
import com.tuanbiao.events.EncodingRulesEvent;
import com.tuanbiao.models.EncodingRulesModel;
import com.tuanbiao.utils.LibCollections;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/7/28 10:13
 * @description:
 */
public class EncodingRulesPresenter extends BasePresenter<EncodingRulesModel, EncodingRulesController.View> implements EncodingRulesController.Presenter {
    private boolean isFromSetting;
    private List<CodeModelType> codeModels;

    public EncodingRulesPresenter(EncodingRulesModel model, EncodingRulesController.View rootView) {
        super(model, rootView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void init(EncodingRulesEvent event) {
        isFromSetting = event.isFromSetting();
        Timber.i("init#" + isFromSetting);
        EventBus.getDefault().removeStickyEvent(event);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        Timber.i("onCreate#" + isFromSetting);
        mModel.queryMillCode(millCodeEntity -> {
            mRootView.showDetails(millCodeEntity);
        });
        mRootView.showFromSetting(isFromSetting);
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public void saveMillCode(CodeModelType codeModelType, String moduleMill, String detonatorMill, String signatureCode, String moduleType) {
        if (null == codeModelType) {
            mRootView.showMessage(R.string.config_encoding_rules_hint);
            return;
        }
        if (moduleMill.length() < 2) {
            moduleMill = "0" + moduleMill;
        }
        if (TextUtils.isEmpty(moduleMill)) {
            mRootView.showMessage(R.string.config_module_mill_code_hint);
            return;
        }
        if (detonatorMill.length() < 2) {
            detonatorMill = "0" + detonatorMill;
        }
        if (TextUtils.isEmpty(detonatorMill)) {
            mRootView.showMessage(R.string.config_detonator_mill_code_hint);
            return;
        }
        if (TextUtils.isEmpty(signatureCode)) {
            mRootView.showMessage(R.string.config_signature_code_hint);
            return;
        }
        if (TextUtils.isEmpty(moduleType)) {
            mRootView.showMessage(R.string.config_module_type_hint);
            return;
        }
        UIDRuleEntity entity = new UIDRuleEntity();
        entity.setRuleName(codeModelType.itemName());
        entity.setRule(codeModelType.getName().getRule());
        entity.setModuleMillCode(moduleMill);
        entity.setCapMillCode(detonatorMill);
        entity.setSignatureCode(signatureCode);
        entity.setModuleTypeCode(moduleType);
        mModel.updateMillCode(entity, new OnBaseResultCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean success) {
                Timber.i("updateMillCode#" + success);
                if (success) {
                    mRootView.showMessage("修改成功");
                    if (!isFromSetting) {
                        if (mModel.getSettingParams().isStandAloneModel()) mRootView.startMain();
                        else mRootView.startLogin();
                    }
                    mRootView.killMyself();
                }
            }
        });
    }

    @Override
    public void showEnCodingRulesDialog() {
        if (null == codeModels) {
            mModel.queryRules(new OnBaseResultCallback<List<UIDRuleEntity>>() {
                @Override
                public void onSuccess(List<UIDRuleEntity> uidRuleEntities) {
                    codeModels = new ArrayList<>(LibCollections.size(uidRuleEntities));
                    if (null != uidRuleEntities) {
                        for (UIDRuleEntity entity : uidRuleEntities) {
                            codeModels.add(new CodeModelType(entity));
                        }
                    }
                    showEnCodingRulesDialog();
                }
            });
        } else {
            mRootView.showEncodingRulesDialog(codeModels);
        }
    }
}
